package com.raise.drive.web.controller;

import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;
import java.io.IOException;
import java.io.FileInputStream;

import com.raise.drive.commons.web.springmvc.view.AbstractFileView;
import com.raise.drive.commons.web.springmvc.view.JpegView;

/**
 * @author Eugene Rozov
 */
public class PhotoController extends AbstractController {

//    private SessionFactory sessionFactory;

    public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        String userId = request.getParameter(WebConstants.USER_ID_PARAM);
//        String type = request.getParameter("type");


//        Session session = null;
        byte[] file = null;
/*        try {
            session = sessionFactory.createSession();
            file = session.getFile(MessageFormat.format("/avatars/{0}_{1}.jpg", userId, type));
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }


        if (file == null || file.length == 0) {
            String path = getServletContext().getRealPath("/");
            FileInputStream s = new FileInputStream(path + "/images/icon/user/user-male_" + type + ".png");
            file = new byte[s.available()];
            s.read(file);
            s.close();
        }
*/

        JpegView view = new JpegView();
        view.setCacheable(true);

        return new ModelAndView(view, AbstractFileView.BYTES_KEY, file);
    }

//    public void setSessionFactory(SessionFactory sessionFactory) {
//        this.sessionFactory = sessionFactory;
//    }

}

